/*
 * $HeadURL: $
 * $Id: $
 * Copyright (c) 2012 by Wodensoft System, all rights reserved.
 */
package com.wodensoft.util.script;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author Borball
 * 
 */
public class HTTPExecutionContext implements ExecutionContext {

    private Map<String, Object> variables;

    public HTTPExecutionContext(HttpServletRequest request) {
        variables = new HashMap<String, Object>();

        readContextFromSession(request.getSession());
        
        readContextFromRequest(request);
    }

    private void readContextFromRequest(HttpServletRequest request) {
        Enumeration<String> attributesInRequest = request.getAttributeNames();
        while (attributesInRequest.hasMoreElements()) {
            String name = attributesInRequest.nextElement();
            Object value = request.getAttribute(name);
            variables.put(name, value);
        }
    }

    private void readContextFromSession(HttpSession session) {
        Enumeration<String> attributesInSession = session.getAttributeNames();
        while (attributesInSession.hasMoreElements()) {
            String name = attributesInSession.nextElement();
            Object value = session.getAttribute(name);

            variables.put(name, value);
        }
    }

    @Override
    public Map<String, Object> getVariableContext() {
        return variables;
    }

}
